//
//  MainNavigationController.m
//  ZhiNengXiaoFu
//
//  Created by duxiu on 2018/7/25.
//  Copyright © 2018年 henanduxiu. All rights reserved.
//

#import "MainNavigationController.h"
#import "LoginViewController.h"
#import "TeacherVC.h"
#import "YueJuanViewController.h"
@interface MainNavigationController ()<ChangeVCDelegate>
@property (nonatomic ,strong) UIViewController *currentVC;
@end

@implementation MainNavigationController

+ (void)load {
    
    UINavigationBar *navbar = [UINavigationBar appearanceWhenContainedIn:self, nil];
    [[UITabBar appearance] setTintColor:TABBARCOLOR];
    [navbar setTitleTextAttributes:
      @{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Semibold" size:18],NSForegroundColorAttributeName:[UIColor blackColor]}];
    NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
    mDict[NSForegroundColorAttributeName] = TABBARMAINTITLECOLOR;
    mDict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    navbar.titleTextAttributes = mDict;
    //去掉黑线
    [navbar setShadowImage:[[UIImage alloc] init]];
}


- (void)viewWillAppear:(BOOL)animated{
    self.navigationBar.hidden = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.backBtn = ({
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        btn.titleLabel.hidden = YES;
        [btn addTarget:self action:@selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        btn.frame = CGRectMake(0, 0, 44, 40);
        btn;
    });
    
    // 重新设置侧滑手势的代理
    __weak typeof(self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = (id)weakSelf;
    }
    [self.view addSubview:self.tabbar_vc.view];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hideBarClick) name:@"HideTabbarNoti" object:nil];
    
}
- (void)hideBarClick{
    self.tabbar_vc.view.hidden = YES;
}
// 开始接收到手势的代理方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    // 判断是否是侧滑相关的手势
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        // 如果当前展示的控制器是根控制器就不让其响应
        if (self.viewControllers.count < 2 ||
            self.visibleViewController == [self.viewControllers objectAtIndex:0]) {
            return NO;
        }
    }
    return YES;
}

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count > 0) {
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.backBtn];
        viewController.view.frame = CGRectMake(TABBARWIGHT, 0, APP_WIDTH-TABBARWIGHT, APP_HEIGHT);
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}
- (void)rootViewSwitch:(int)index andArray:(NSMutableArray *)viewArray{
    //  点击处于当前页面的按钮,直接跳出
    UIViewController *vc = viewArray[index];
    if (self.currentVC == viewArray[index]) {
         return;
    }else{
        [self addChildViewController:vc];
        [vc.view setFrame:CGRectMake(80,0,APP_WIDTH-80,APP_HEIGHT)];
        [self.view addSubview:vc.view];
        //替换view
        [vc didMoveToParentViewController:self];
        [self.currentVC willMoveToParentViewController:nil];
        [self.currentVC removeFromParentViewController];
        [self.currentVC.view removeFromSuperview];
        
    }
    
    self.currentVC = vc;
}
#pragma mark - 点击首页的个别项跳转到相应的模块
- (void)rootViewSwitch:(int)index andArray:(NSMutableArray *)viewArray andModel:(TheNewWorkListModel *)model{
    
    UIViewController *vc = viewArray[index];
    if (self.currentVC == viewArray[index]) {
        return;
    }else{
        [self addChildViewController:vc];
        [vc.view setFrame:CGRectMake(80,0,APP_WIDTH-80,APP_HEIGHT)];
        [self.view addSubview:vc.view];
        //替换view
        [vc didMoveToParentViewController:self];
        [self.currentVC willMoveToParentViewController:nil];
        [self.currentVC removeFromParentViewController];
        [self.currentVC.view removeFromSuperview];
        
    }
    //
    self.currentVC = vc;
    //教学
    if (index == 5) {
//        TeacherVC *VC = (TeacherVC *)vc;
////        TheNewWorkListModel *m = (TheNewWorkListModel *)model;
//      //  NSLog(@"得到的model %@",model);
//        //前往作业详情
//        [VC goViewOfZuoYeModeul:model];
        YueJuanViewController *yuejuanVC = (YueJuanViewController *)vc;
        [yuejuanVC selectItem];
    }
}
- (TabbarVC *)tabbar_vc{
    if (!_tabbar_vc) {
        TabbarVC *tab = [[TabbarVC alloc]init];
        tab.delegate = self;
        tab.view.frame = CGRectMake(0, 0, 80, APP_HEIGHT);
        tab.view.backgroundColor = [UIColorChange colorwithHexString:@"#364354"];
        _tabbar_vc = tab;
    }
    return _tabbar_vc;
}
- (void)backBtnClicked:(UIButton *)btn {
    
    [self popViewControllerAnimated:YES];
}

@end
